package psn.hui.stat.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.aspectj.apache.bcel.classfile.Module.Export;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import psn.hui.bus.domain.Customer;
import psn.hui.bus.domain.Rent;
import psn.hui.bus.service.CustomerService;
import psn.hui.bus.service.RentService;
import psn.hui.bus.vo.CustomerVo;
import psn.hui.stat.domain.BaseEntity;
import psn.hui.stat.service.StatService;
import psn.hui.stat.utils.ExportCustomerUtils;
import psn.hui.stat.utils.ExportRentUtils;
import psn.hui.sys.utils.DataGridView;

/**
 * 统计分析控制器
 * @author hui
 *
 */
@Controller
@RequestMapping("stat")
public class StatController {
	
	/**
	 * @return
	 */
	@Autowired
	private StatService statService;
	
	@Autowired
	private CustomerService customerService;
	
	@Autowired
	private RentService rentService;
	
	/**
	 * 跳转到客户地区统计页面
	 */
	@RequestMapping("toCustomerAreaStat")
	public String toCustomerAreaStat() {
		return "stat/customerAreaStat";
	}
	/**
	 * 	获取客户地区统计数据json(BaseEntity数组)
	 * @return
	 */
	@RequestMapping("loadCustomerAreaStatJson")
	@ResponseBody
	public List<BaseEntity> loadCustomerAreaStatJson() {
		return statService.loadCustomerAreaStatJson();
	}
	
	/**
	 * 跳转到年度业务员销售额统计页面
	 * @return
	 */
	@RequestMapping("toSalesmanAnnualSalesStat")
	public String toSalesmanAnnualSalesStat() {
		return "stat/salesmanAnnualSalesStat";
	}
	
	/**
	 * 获取年度业务员销售额数据
	 * @param year
	 * @return
	 */
	@RequestMapping("loadSalesmanAnnualSalesStatJson")
	@ResponseBody
	public Map<String, Object> loadSalesmanAnnualSalesStatJson(Integer year){
		return statService.loadSalesmanAnnualSalesStatJson(year);
	}
	
	/**
	 * 跳转到公司年度月份销售额统页面
	 * @return
	 */
	@RequestMapping("toAnnualMonthlySalesStat")
	public String toAnnualMonthlySalesStat() {
		return "stat/annualMonthlySalesStat";
	}
	
	/**
	 * 获取公司年度月份销售额统计数据
	 * @param year
	 * @return
	 */
	@RequestMapping("loadAnnualMonthlySalesStatJson")
	@ResponseBody
	public List<Double> loadAnnualMonthlySalesStatJson(Integer year){
		return statService.loadAnnualMonthlySalesStatJson(year);
	}
	/**
	 * 	导出用户数据
	 */
	@RequestMapping("exportCustomer")
	public void exportCustomer(CustomerVo customerVo,HttpServletResponse response) {
		try {
			//查询客户数据
			List<Customer> customers = customerService.queryAllCustomerForList(customerVo);
			String fileName = "客户数据.xls";
			String sheetName = "客户数据";
			//导出数据
			ExportCustomerUtils.exportCustomer(customers,fileName,sheetName,response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 导出出租单
	 * @param rentid
	 */
	@RequestMapping("exportRent")
	public void exportRent(String rentid,HttpServletResponse response) {
		try {
			Rent rent = rentService.queryRentByRentId(rentid);
			Customer customer = customerService.selectByIdentity(rent.getIdentity());
			String fileName = "出租单-"+customer.getCustname()+".xls";
			String sheetName = "出租单-"+customer.getCustname();
			ExportRentUtils.exportRent(rent,customer, fileName, sheetName, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
